package net.example.activity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import net.example.model.KeyValuePairModel;
import net.example.ggtalk.R;

import java.util.ArrayList;
import java.util.List;

public class KeyValuePairAdpter extends BaseAdapter {

    private List<KeyValuePairModel> modelList=new ArrayList<KeyValuePairModel>();
    private Context context;
    private boolean iconExisted=false;//每行中是否存在第一个ICON 图标

    public KeyValuePairAdpter(List<KeyValuePairModel> modelList,Context context)
    {
        this.modelList=modelList;
        this.context=context;
    }
    public KeyValuePairAdpter(List<KeyValuePairModel> modelList,Context context,boolean iconExisted)
    {
        this.modelList=modelList;
        this.context=context;
        this.iconExisted=iconExisted;
    }

    @Override
    public int getCount() {
        return this.modelList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.modelList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        try {
            KeyValuePairAdpter.ViewHolder holder;
            if (view == null) {
                view = LayoutInflater.from(context).inflate(
                        R.layout.key_value_pair_item, parent, false);
                holder = new KeyValuePairAdpter.ViewHolder(view);
                view.setTag(holder);
            } else {
                holder = (KeyValuePairAdpter.ViewHolder) view.getTag();
            }
            KeyValuePairModel model = this.modelList.get(position);
            if (model.IsShowRigthBtn) {
                holder.iv_btn_right.setVisibility(View.VISIBLE);
            } else {
                holder.iv_btn_right.setVisibility(View.INVISIBLE);
            }
            if (this.iconExisted) {
                if (model.ImageResourceID != 0) {
                    holder.iv_icon.setVisibility(View.VISIBLE);
                    holder.iv_icon.setImageResource(model.ImageResourceID);
                } else {
                    holder.iv_icon.setVisibility(View.INVISIBLE);
                }
            }
            holder.tv_key.setText(model.Key);
            holder.tv_value.setText(model.Value);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return view;
    }

//    @Override
//    public boolean isEnabled(int position) {
//        KeyValuePairModel model= this.modelList.get(position);
//        if(!model.IsShowRigthBtn)
//        {
//            return false;
//        }
//        return super.isEnabled(position);
//    }
//
//    @Override
//    public boolean areAllItemsEnabled() {
//        return true;
//    }

    private class ViewHolder
    {
        public TextView tv_key;
        public TextView tv_value;
        public ImageView iv_icon;
        public ImageView iv_btn_right;
        public ViewHolder(View view)
        {
            this.tv_key=(TextView) view.findViewById(R.id.list_item_key);
            this.tv_value=(TextView) view.findViewById(R.id.list_item_value);
            this.iv_btn_right=(ImageView) view.findViewById(R.id.btn_right);
            this.iv_icon=(ImageView)view.findViewById(R.id.kvp_icon);
        }
    }
}
